//
//  DXChannel.swift
//  nineteenfloor
//
//  Created by 大郗 on 16/3/6.
//  Copyright © 2016年 大郗. All rights reserved.
//

import UIKit

class DXChannel: NSObject {

	// 频道id
	var tid : String? {
		didSet {
			if (tid == "T1348647853363") {
				URLString = NSString(format: "article/headline/%@/0-40.html", tid!) as String
			} else {
				URLString = NSString(format: "article/list/%@/0-40.html", tid!) as String
			}
		}
	}

	// 频道对应的URLString
	var URLString: String?
	// 频道名称
	var tname: String?
	// 频道模型的数组
	class func channels() -> NSArray? {
		let channels = NSMutableArray()
		let fileURL = NSBundle.mainBundle().URLForResource("topic_news.json", withExtension: nil)
		let fileData = NSData(contentsOfURL: fileURL!)
		let dict = try? NSJSONSerialization.JSONObjectWithData(fileData!, options: [])
		let dictArray = dict!["tList"]
		for dict in dictArray as! NSArray {
			let channel = DXChannel.init(dict: dict as! [String : AnyObject])
			channels.addObject(channel)
		}
		return channels.copy() as? NSArray
	}

	var properties: NSArray {
		return ["tid", "tname"]
	}

	init(dict: [String: AnyObject]) {
		super.init()
//        setValuesForKeysWithDictionary(dict)
		for propertyName in self.properties {
			if dict[propertyName as! String] != nil
			{
				setValue(dict[propertyName as! String], forKey: propertyName as! String)
			}
		}
	}

	override func setValue(value: AnyObject?, forUndefinedKey key: String) { }

	override var description: String {
		let keys = ["uid", "expires_in", "access_token", "avatar_large", "screen_name"]
		return dictionaryWithValuesForKeys(keys).description
	}
}
